using Godot;
using System;
using Legendofthebrave.Scripts.Characters;
public partial class Bora : Enemy,StateAction
{
    private RayCast2D WallChecker;
    private RayCast2D PlayerChecker;
    private RayCast2D FloorChecker;
    private Timer CalmDownTimer;

    public override void _Ready()
    {
        base._Ready();
 
        WallChecker = GetNode<RayCast2D>("Graphics/WallChecker");
        PlayerChecker = GetNode<RayCast2D>("Graphics/PlayerChecker");
        FloorChecker = GetNode<RayCast2D>("Graphics/FloorChecker");
        CalmDownTimer = GetNode<Timer>("CalmDownTimer");
    
    }

    
    public void TickPhysics(State state,double delta)
    {
        GD.Print(StateMachineNode.CurrentState);
        switch (state)
        {
            case State.Idle:
                Move(0.0f,delta);
                break;
            case State.Walk:
                Move(MaxSpeed / 3.0f,delta);
                if (WallChecker.IsColliding() || !FloorChecker.IsColliding())
                    direction = (Direction)((int)direction * -1);
                break;
            case State.Run:
                if (WallChecker.IsColliding() || !FloorChecker.IsColliding())
                    direction = (Direction)((int)direction * -1);
                Move(MaxSpeed,delta);
                if(PlayerChecker.IsColliding())
                    CalmDownTimer.Start();
                break;
            
        }
       
    }

    
    
    public State GetNextState(State state)
    {
        if (PlayerChecker.IsColliding())
            return State.Run;

        switch (state)
        {
            case State.Idle:
                if (StateMachineNode.StateTime > 2.0f)
                    return State.Walk;
                break;
            case State.Walk:
                if (WallChecker.IsColliding() || !FloorChecker.IsColliding())
                    return State.Idle;
                break;
            case State.Run:
                if (CalmDownTimer.IsStopped())
                    return State.Walk;
                break;
            
        }
        return state;
    }
    
    public void TransitionState(State from, State to)
    {
        switch (to)
        {
            case State.Idle:
                AnimationPlayerNode.Play("Idle");
                if (WallChecker.IsColliding())
                    direction = (Direction)((int)direction * -1);
                break;
            case State.Walk:
                AnimationPlayerNode.Play("Walk");
                if(!FloorChecker.IsColliding())
                    direction = (Direction)((int)direction * -1);
                break;
            case State.Run:
                AnimationPlayerNode.Play("Run");
                break;
            
        }

    }
    

}
